<?php
	$rosterIds = array();
	$time = strtotime("2011-05-22");
	$ts_firstDay = mktime(0, 0, 0, date("m",$time), 1, date("y",$time));
	$dt_firstDay = date("Y-m-d",$ts_firstDay);
	$ts_lastDay = mktime(0, 0, 0, date("m",$time)+1, 0, date("y",$time));
	$dt_lastDay = date("Y-m-d",$ts_lastDay);
	
	try{

		$db = @new mysqli( $db_host,$db_user,$db_pass,$db_name );
		if ( !mysqli_connect_errno() ){
			
			$sql = "SELECT * FROM `dienstplan` WHERE `datum` = '$dt_firstDay'";
		    $rosters = $db->query( $sql );
			    
		    if( $rosters->num_rows > 0 ){
					    	
				while( $roster = $rosters->fetch_object() ){
					
					array_push($rosterIds, $roster->id);
				}

			}else{
					  			    
				throw new Exception( 'Die Abfrage '.$sql.' liefert keine Ergebnisse' );
					
			}
			
			$rosters->close();
			
			for($i=0; $i<count($rosterIds); $i++){
			
				$sql = "SELECT * FROM `dienst` WHERE `dienstplanid` = '".$rosterIds[$i]."'";
			    $duties = $db->query( $sql );
				    
			    if( $duties->num_rows > 0 ){
					//TABLE RENDER
				 	$tbl_date = '<table class="datelist">'."\n";
					$tbl_date.= "\t\t\t\t".'<tr>'."\n";
					switch(date('m',$ts_firstDay)){
						case '01':
							$monthname = 'Januar';
							break;
						case '02':
							$monthname = 'Februar';
							break;
						case '03':
							$monthname = 'März';
							break;
						case '04':
							$monthname = 'April';
							break;
						case '05':
							$monthname = 'Mai';
							break;
						case '06':
							$monthname = 'Juni';
							break;
						case '07':
							$monthname = 'Juli';
							break;
						case '08':
							$monthname = 'August';
							break;
						case '09':
							$monthname = 'September';
							break;
						case '10':
							$monthname = 'Oktober';
							break;
						case '11':
							$monthname = 'November';
							break;
						case '12':
							$monthname = 'Dezember';
					}
					$tbl_date.= "\t\t\t\t\t".'<td class="description"><b>'.$monthname.'</b><br />'.date('Y',mktime(0, 0, 0, date("m",$time), date("d",$time), date("y",$time))).'</td>'."\n";
					for($i = 0; $i < date("d",$ts_lastDay); $i++){
						$day = date('D',mktime(0, 0, 0, date("m",$ts_firstDay),date("d",$ts_firstDay) + $i, date("y",$time)));
						switch($day){
							case 'Mon':
								$weekday = 'Mo';
								break;
							case 'Tue':
								$weekday = 'Di';
								break;
							case 'Wed':
								$weekday = 'Mi';
								break;
							case 'Thu':
								$weekday = 'Do';
								break;
							case 'Fri':
								$weekday = 'Fr';
								break;
							case 'Sat':
								$weekday = 'Sa';
								break;
							case 'Sun':
								$weekday = '<b>So</b>';
						}
						$tbl_date.= "\t\t\t\t\t".'<td>'.$weekday.'<br />'.date('d',mktime(0, 0, 0, date("m",$time), +1 +$i, date("y",$time))).'</td>'."\n";
					}
					$tbl_date.= "\t\t\t\t\t".'<td class="actual_state">&nbsp;<br />Ist</td>'."\n";
					$tbl_date.= "\t\t\t\t\t".'<td class="target_state">&nbsp;<br />Soll</td>'."\n";
					$tbl_date.= "\t\t\t\t\t".'<td class="overtime_total">&nbsp;<br />ÜSt.</td>'."\n";
					$tbl_date.= "\t\t\t\t".'</tr>'."\n";
					$tbl_date.= "\t\t\t".'</table>'."\n";
					
					$tbl_employees = '<table class="employees">'."\n";
					while( $duty = $duties->fetch_object() ){
						
						$tbl_employees.= "\t\t\t\t".'<tr>'."\n";
						$tbl_employees.= "\t\t\t\t\t".'<td class="description">'.$duty->mitarbeiterid.'</td>'."\n";
						echo date("d",strtotime($duty->datum))."<br />\n";
						
						for($j = 0; $j < date("d",$ts_lastDay); $j++){
							if( date("d",strtotime($duty->datum)) == $j){
								$tbl_employees.= "\t\t\t\t\t".'<td>'.$duty->diensttypid.'</td>'."\n";
							}else{
								$tbl_employees.= "\t\t\t\t\t".'<td>&nbsp;</td>'."\n";
							}
						}
						$tbl_employees.= "\t\t\t\t\t".'<td class="actual_state">0</td>'."\n";
						$tbl_employees.= "\t\t\t\t\t".'<td class="target_state">0</td>'."\n";
						$tbl_employees.= "\t\t\t\t\t".'<td class="overtime_total">0</td>'."\n";
						$tbl_employees.= "\t\t\t\t".'</tr>'."\n";
					}
					
					$tbl_employees.= "\t\t\t".'</table>'."\n";
		
				}else{
						  			    
					throw new Exception( 'Die Abfrage '.$sql.' liefert keine Ergebnisse' );
						
				}
				
				$duties->close();
					
			}    
		}else{
				
			throw new Exception( 'Die Datenbank konnte nicht erreicht werden. Folgender Fehler trat auf: <strong>' .mysqli_connect_errno(). ' : ' .mysqli_connect_error(). '</strong>' );
				  
		}
			
		$db->close();
		
	}catch( Exception $e ){
		
		echo '<pre><b>Exception</b>'."\n"; 
		echo $e->getMessage();
		echo '</pre>';
		   
	}
	
?>
			
			<h3>Wohnbereich x</h3>
			<div class="roster">
				<?php echo $tbl_date; ?>
				<?php echo $tbl_employees; ?>
			</div>